package io.aircraft.modules.wms.controller;

import io.aircraft.common.annotation.LogOperation;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.Result;
import io.aircraft.common.validator.AssertUtils;
import io.aircraft.common.validator.ValidatorUtils;
import io.aircraft.common.validator.group.AddGroup;
import io.aircraft.common.validator.group.DefaultGroup;
import io.aircraft.common.validator.group.UpdateGroup;
import io.aircraft.modules.wms.dto.OrderDetailDTO;
import io.aircraft.modules.wms.service.OrderDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 出库明细
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-11-16
 */
@RestController
@RequestMapping("wms/order-detail")
public class OrderDetailController {
    @Autowired
    private OrderDetailService orderDetailService;

    @GetMapping("page")
    @RequiresPermissions("wms:order-detail:page")
    public Result<PageData<OrderDetailDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<OrderDetailDTO> page = orderDetailService.page(params);

        return new Result<PageData<OrderDetailDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:order-detail:info")
    public Result<OrderDetailDTO> get(@PathVariable("id") Long id){
        OrderDetailDTO data = orderDetailService.get(id);

        return new Result<OrderDetailDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:order-detail:save")
    public Result save(@RequestBody OrderDetailDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        orderDetailService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:order-detail:update")
    public Result update(@RequestBody OrderDetailDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        orderDetailService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:order-detail:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        orderDetailService.delete(ids);

        return new Result();
    }

}